//
//  ConGotoCommentController.m
//  Shengzhuli
//
//  Created by FXGuo on 2017/4/15.
//  Copyright © 2017年 kamangkeji. All rights reserved.
//

#import "ConGotoCommentController.h"
#import "ConSingleGoodsDetailCell.h"
#import "CommentCell.h"
#import "MBProgressHUD+MJ.h"
#import "SingleListNetWork.h"
#import "SZLAccountHelper.h"

@interface ConGotoCommentController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *sourceTab;

@end

@implementation ConGotoCommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品评论";
    [self.view addSubview:self.sourceTab];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 193;
    }
    return 133;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        ConSingleGoodsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ConSingleGoodsDetailCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.listModel;
        return cell;
    }
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([CommentCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.listModel;
    __WEAK
    cell.commentBlock = ^{
        [weakSelf publicComment];
    };
    return cell;
}
- (void)publicComment
{
    [self.view endEditing:YES];
    if (self.listModel.commentContent.length<1) {
        [MBProgressHUD showError:@"评论内容不能为空"];
        return;
    }
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
    parame[@"orderNo"] = self.listModel.orderNo;
    parame[@"content"] = self.listModel.commentContent;
    [self showClearBgWaiter];
    [SingleListNetWork singleCommentWithParame:parame response:^(BOOL success, NSString *msg) {
        [self hideWaiter];
        if (success) {
            [MBProgressHUD showSuccess:@"评价成功" toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.successBlock) {
                    self.successBlock();
                }
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [MBProgressHUD showError:msg toView:self.view];
        }
    }];
}
- (UITableView *)sourceTab
{
    if (!_sourceTab) {
        _sourceTab = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH)];
        _sourceTab.dataSource = self;
        _sourceTab.delegate = self;
        _sourceTab.tableFooterView = [[UIView alloc]init];
        _sourceTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([ConSingleGoodsDetailCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([ConSingleGoodsDetailCell class])];
        [_sourceTab registerNib:[UINib nibWithNibName:NSStringFromClass([CommentCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([CommentCell class])];
    }
    return _sourceTab;
}

@end
